package com.freight.user.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.freight.core.common.exception.FreightException;
import com.freight.core.common.redis.RedisTemplateHelper;
import com.freight.core.common.utils.SecurityUtil;
import com.freight.core.common.vo.SearchVo;
import com.freight.core.entity.User;
import com.freight.core.service.UserService;
import com.freight.file.constants.FileConstant;
import com.freight.file.entity.File;
import com.freight.file.service.FileService;
import com.freight.file.util.DriverOCRUtil;
import com.freight.file.util.FileUtils;
import com.freight.file.util.MyFileUtils;
import com.freight.file.vo.PicFileVo;
import com.freight.msg.service.MessageService;
import com.freight.user.dao.DriverLicenseDao;
import com.freight.user.entity.DriverLicense;
import com.freight.user.entity.DriverUser;
import com.freight.user.mapper.DriverVoMapper;
import com.freight.user.service.DriverLicenseService;
import com.freight.user.service.DriverUserService;
import com.freight.user.service.IdCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 驾驶证接口实现
 *
 * @author huyingtai
 */
@Slf4j
@Service
public class DriverLicenseServiceImpl implements DriverLicenseService {

    @Autowired
    private DriverLicenseDao driverLicenseDao;

    @Autowired
    SecurityUtil securityUtil;

    @Autowired
    private RedisTemplateHelper redisTemplate;

    @Autowired
    private DriverUserService driverUserService;

    @Autowired
    IdCardService idCardService;

    @Autowired
    FileService fileService;

    @Autowired
    UserService userService;

    @Override
    public DriverLicenseDao getRepository() {
        return driverLicenseDao;
    }

    @Override
    public DriverLicense getDriverLicenseInfo(Long userId) {
        securityUtil.getCurrUser();

        DriverUser driverUser = driverUserService.findByUserId(userId);
        if (driverUser != null && driverUser.getDriverId() != null) {
            DriverLicense res = driverLicenseDao.getOne(driverUser.getDriverId());
            return res;
        }
        return new DriverLicense();
    }

    @Override
    public Page<DriverLicense> findByCondition(DriverLicense driverLicense, SearchVo searchVo, Pageable pageable) {

        return driverLicenseDao.findAll(new Specification<DriverLicense>() {
            @Nullable
            @Override
            public Predicate toPredicate(Root<DriverLicense> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {

                // TODO 可添加你的其他搜索过滤条件 默认已有创建时间过滤
                Path<Date> createTimeField = root.get("createTime");

                List<Predicate> list = new ArrayList<>();

                // 创建时间
                if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
                    Date start = DateUtil.parse(searchVo.getStartDate());
                    Date end = DateUtil.parse(searchVo.getEndDate());
                    list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
                }

                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        }, pageable);
    }
    @Override
    public PicFileVo upLoadDriverLicense(MultipartFile file, String fileConstant, Long userId) {
        User u = securityUtil.getCurrUser();
        java.io.File picFile = null;
        try {
            picFile = MyFileUtils.convert(file);
        } catch (IOException e) {
            throw new FreightException("解析驾驶证文件异常");
        }
        log.info("---------------------------------------------------");
        String image_64 = FileUtils.baiud_img_file(picFile);
        File sfile = fileService.saveFileByUserId(file, fileConstant);
        picFile.delete();
        PicFileVo res = new PicFileVo();

        switch (fileConstant) {
            case FileConstant.DRIVER_FONT_PIC: {
                res = DriverOCRUtil.baiDuJustDriver(image_64,redisTemplate);
                if(res==null){
                    throw new FreightException("图片不清晰： 请重新上传图片");
                }
                res.setFileId(sfile.getId());
                res.setFilepath(sfile.getUrl());
                break;
            }
            case FileConstant.ROAD_OPERATION_PERMIT: {

                res.setFileId(sfile.getId());
                res.setFilepath(sfile.getUrl());
                break;
            }
            default: {
                throw new FreightException("文件格式异常");
            }
        }
        return res;
    }


}